﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToWeb.CApExportToWebViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.ExportToWeb;

public class CApExportToWebViewModel : INotifyPropertyChanged
{
  private int m_selectedLOD = 2;
  private string m_outputFileName = "";
  private string m_outputNameInTC = "";
  private bool m_canExecute;
  private bool m_loadBrowser = true;
  private bool? m_exportSimulationCheckedState;
  private bool? m_saveToFileCheckedState;
  private bool? m_saveToServerCheckedState;
  private ITxOperation m_operation;
  private ITxOperation m_operationToAttach;
  private List<string> m_lodItemsList;
  public bool m_isOnRuntime;
  public bool m_isOnAutoTest;

  public CApExportToWebViewModel()
  {
    this.CanExecute = this.m_canExecute;
    this.LoadBrowser = this.m_loadBrowser;
    this.SaveToFileCheckedState = new bool?(true);
    this.SaveToServerCheckBox = new bool?(false);
    this.CreateLodItemsList();
  }

  public List<string> LodItemsList
  {
    get => this.m_lodItemsList;
    set
    {
      this.m_lodItemsList = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (LodItemsList)));
    }
  }

  public bool? SaveToFileCheckedState
  {
    get => this.m_saveToFileCheckedState;
    set
    {
      this.m_saveToFileCheckedState = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (SaveToFileCheckedState)));
    }
  }

  public bool? SaveToServerCheckBox
  {
    get => this.m_saveToServerCheckedState;
    set
    {
      this.m_saveToServerCheckedState = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (SaveToServerCheckBox)));
    }
  }

  public bool? ExportSimulationCheckedState
  {
    get => this.m_exportSimulationCheckedState;
    set
    {
      this.m_exportSimulationCheckedState = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (ExportSimulationCheckedState)));
    }
  }

  public string OutputFileName
  {
    get => this.m_outputFileName;
    set
    {
      this.m_outputFileName = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (OutputFileName)));
    }
  }

  public string OutputNameInTC
  {
    get => this.m_outputNameInTC;
    set
    {
      this.m_outputNameInTC = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (OutputNameInTC)));
    }
  }

  public int SelectedLOD
  {
    get => this.m_selectedLOD;
    set
    {
      this.m_selectedLOD = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (SelectedLOD)));
    }
  }

  public bool CanExecute
  {
    get => this.m_canExecute;
    set
    {
      this.m_canExecute = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (CanExecute)));
    }
  }

  public bool LoadBrowser
  {
    get => this.m_loadBrowser;
    set
    {
      this.m_loadBrowser = value;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (LoadBrowser)));
    }
  }

  public ITxOperation Operation
  {
    get => this.m_operation;
    set
    {
      this.m_operation = value;
      if (this.m_isOnRuntime)
        return;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (Operation)));
    }
  }

  public ITxOperation OperationToAttach
  {
    get => this.m_operationToAttach;
    set
    {
      this.m_operationToAttach = value;
      if (this.m_isOnRuntime)
        return;
      this.OnPropertyChanged(new PropertyChangedEventArgs(nameof (OperationToAttach)));
    }
  }

  public bool BrowseOutput()
  {
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "html files (*.html)|*.html";
    saveFileDialog.AddExtension = true;
    saveFileDialog.OverwritePrompt = true;
    saveFileDialog.InitialDirectory = this.GetOutputDirectory();
    saveFileDialog.FileName = this.PrepareDefaultFileName();
    if (saveFileDialog.ShowDialog() != DialogResult.OK)
      return false;
    this.OutputFileName = saveFileDialog.FileName.EndsWith(".html") ? saveFileDialog.FileName : saveFileDialog.FileName + ".html";
    this.OutputNameInTC = Path.GetFileNameWithoutExtension(this.OutputFileName);
    return true;
  }

  private string GetOutputDirectory()
  {
    string outputDirectory = "";
    if (!string.IsNullOrEmpty(this.OutputFileName))
    {
      try
      {
        outputDirectory = Directory.GetParent(this.OutputFileName).FullName;
      }
      catch (Exception ex)
      {
      }
    }
    return outputDirectory;
  }

  private string PrepareDefaultFileName()
  {
    string str = "";
    if (TxApplication.ActiveDocument.CurrentStudy != null)
      str = string.IsNullOrEmpty(this.OutputFileName) ? TxApplication.ActiveDocument.CurrentStudy.Caption + "_WebView.html" : this.OutputFileName;
    else if (TxTcUtilities.IsTcAnyPlatform)
      str = !string.IsNullOrEmpty(this.OutputFileName) ? Path.GetFileNameWithoutExtension(this.OutputFileName) : TxApplication.ActiveDocument.PhysicalRoot.Name + "_WebView.html";
    return str;
  }

  private bool IsOutputFileNameValid()
  {
    bool? fileCheckedState = this.SaveToFileCheckedState;
    bool flag = true;
    return fileCheckedState.GetValueOrDefault() == flag & fileCheckedState.HasValue && this.OutputFileName != "";
  }

  private bool IsOutputNameInTCValid()
  {
    bool? toServerCheckBox = this.SaveToServerCheckBox;
    bool flag = true;
    return toServerCheckBox.GetValueOrDefault() == flag & toServerCheckBox.HasValue && this.OutputNameInTC != "" && this.OperationToAttach != null;
  }

  private bool IsExportSimulationSelectionValid()
  {
    bool? simulationCheckedState1 = this.ExportSimulationCheckedState;
    bool flag1 = false;
    if (simulationCheckedState1.GetValueOrDefault() == flag1 & simulationCheckedState1.HasValue)
      return true;
    if (this.Operation == null)
      return false;
    bool? simulationCheckedState2 = this.ExportSimulationCheckedState;
    bool flag2 = true;
    return simulationCheckedState2.GetValueOrDefault() == flag2 & simulationCheckedState2.HasValue;
  }

  public bool IsOutputValid()
  {
    bool flag1 = true;
    bool? fileCheckedState = this.SaveToFileCheckedState;
    bool flag2 = true;
    if (fileCheckedState.GetValueOrDefault() == flag2 & fileCheckedState.HasValue && this.OutputFileName == "")
      flag1 = false;
    if (flag1)
    {
      bool? toServerCheckBox = this.SaveToServerCheckBox;
      bool flag3 = true;
      if (toServerCheckBox.GetValueOrDefault() == flag3 & toServerCheckBox.HasValue && (this.OutputNameInTC == "" || this.OperationToAttach == null))
        flag1 = false;
    }
    return flag1;
  }

  public void CheckCanExecute()
  {
    this.CanExecute = this.IsOutputValid();
    if (!this.CanExecute)
      return;
    this.CanExecute = (this.IsOutputFileNameValid() || this.IsOutputNameInTCValid()) && this.IsExportSimulationSelectionValid();
  }

  private void CreateLodItemsList()
  {
    this.LodItemsList = new List<string>();
    this.LodItemsList.Add(ExportToWebStringTable.WINDOW_LOD_HIGH);
    this.LodItemsList.Add(ExportToWebStringTable.WINDOW_LOD_MEDIUM);
    this.LodItemsList.Add(ExportToWebStringTable.WINDOW_LOD_LOW);
  }

  public event PropertyChangedEventHandler PropertyChanged;

  public void OnPropertyChanged(PropertyChangedEventArgs e)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, e);
  }

  public enum LOD_VALUE
  {
    HIGH,
    MEDIUM,
    LOW,
  }
}
